<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * share_page_model.php
 * 分享/下载页面
 * @author  :  Skiychan <developer@zzzzy.com>
 * @created :  2015/4/16
 * @modified:  
 * @version :  0.0.1
 */
class Share_page_model extends MY_Model {


    public function __construct() {
        parent::__construct();
    }

    /**
     * 通过分享码获取分享信息
     * @param $code 分享码
     * @return mixed
     */
    public function get_share_link($code) {
        if (empty($code)) return NULL;
        
        //$where = 'share_code collate utf8_bin = "'.$code.'"';
        $this->db->where("share_code",$code);
        $this->db->limit(1);
        return $this->db->get(self::TB_SHARE_LINK)->row_array();
    }

     /**
      * @param $id 分享id
      * @param $type 类型1为访问数 2为下载数
      * @return CI_DB_active_record|null
      */
    public function update_count($id, $type) {
        if (empty($id) || empty($type)) return NULL;

        $field = $type == 1 ? 'visited_count' : 'download_count';
        $this->db->set($field, "{$field}+1", FALSE);
        $this->db->where('id', $id);
        //更新访问/下载数
        return $this->db->update(self::TB_SHARE_LINK);
    }

    /**
     * 通过share_id 获取分享列表数据
     * @param $where 条件
     * @return bool
     */
    public function get_share_list($where) {
        if (empty($where)) return;

        return $this->db->get_where(self::TB_SHARE_LIST, $where)->result_array();
    }

    /**
     * 取文件夹路径
     * @param string $where
     * @param string $pids
     * @param string $field
     * @return mixed
     */
    public function get_folder_path($where="", $pids="", $field="*") {
        //SELECT id,pid,id as uuid,name,creation_ts from u_folders WHERE id in (172,188,189)
        $this->db->select($field);
        if (! empty($where)) {
            if (! is_array($where)) {
                $where = explode(",", $where);
            }
            $this->db->where($where);
        }
        if (! empty($pids)) {
            if (! is_array($pids)) {
                $pids = explode(',', $pids);
            }
            if (count($pids) == 1) {
                $this->db->where(array("id" => $pids[0]));
            } else {
                $this->db->where_in("id", $pids);
            }
        }
        return $this->db->get(self::TB_FOLDER)->result_array();
    }

    /**
     * 通过一组文件夹查询文件
     * @param $uid
     * @param $fids 文件夹编号
     * @param string $file_fields 查询的文件字段
     * @param $type int 默认1.exe,2.ek (下载的格式)
     * @param $m_path bool 默认否 是否查询文件路径
     * @return array || null
     */
    public function get_folder_files($uid, $fids, $file_fields="", $type=1, $m_path=false) {
        if(empty($uid) || (empty($fids))) return;

        if (! empty($file_fields)) {
            $file_fields .= ",f.id,f.`name`,,f.folder_id,f.ext_name,s.guid";
        } else {
            $file_fields = "f.id,f.`name`,f.folder_id,f.ext_name,s.guid";
        }

        $this->db->join(self::TB_SECURE_FILES." s", "f.id = s.fid AND s.type = ".$type, "LEFT");

        $where['f.uid'] = $uid;
        if (is_array($fids)) {
            if (count($fids) > 1) {
                $this->db->where_in("f.folder_id", $fids);
            } else {
                $where["f.folder_id"] = $fids[0];
                $this->db->limit(1);
            }
        } else {
            $where["f.folder_id"] = $fids;
            $this->db->limit(1);
        }

        if (true == $m_path) {
            $this->db->join(self::TB_FOLDER . " o", "f.folder_id = o.id", "LEFT");
        } else {
            $where['f.access_denied'] = 0;
            $where['f.deleted'] = 0;
        }

        $this->db->where($where);
        $this->db->select($file_fields);
        $this->db->order_by('f.folder_id', "asc");
        $query = $this->db->from(self::TB_FILE." f")->get();

        return $query->result_array();
    }

    /**
     * 获取一组文件的信息
     * @param $uid
     * @param $fids
     * @param string $file_fields
     * @param int $type 默认类型1exe, 2ek
     * @param bool|false $m_path 是否需要文件所在目录的path
     * @return null
     */
    public function get_files_info($uid, $fids, $file_fields="", $type=1, $m_path=false) {
        if(empty($uid) || (empty($fids))) return NULL;

        if (! empty($file_fields)) {
            $file_fields .= ",f.id,f.`name`,f.folder_id,f.ext_name,s.guid";
        } else {
            $file_fields = "f.id,f.`name`,f.folder_id,f.ext_name,s.guid";
        }

        $this->db->join(self::TB_SECURE_FILES." s", "f.id = s.fid AND s.type = ".$type, "LEFT");
        $where['f.uid'] = $uid;

        //文件id
        if (is_array($fids)) {
            if (count($fids) > 1) {
                $this->db->where_in("f.id", $fids);
            } else {
                $where["f.id"] = $fids[0];
                $this->db->limit(1);
            }
        } else {
            $where["f.id"] = $fids;
            $this->db->limit(1);
        }

        //如果获取path
        if (true == $m_path) {
            $this->db->join(self::TB_FOLDER." o", "f.folder_id = o.id", "LEFT");
        } else {
            $where['f.access_denied'] = 0;
            $where['f.deleted'] = 0;
        }

        $this->db->where($where);
        $this->db->select($file_fields);
        $this->db->order_by('f.id', "asc");
        $query = $this->db->from(self::TB_FILE." f")->get();

        return $query->result_array();
    }

    public function check_file_in_share($fid,$code)
    {
        //$where = 'link.share_code collate utf8_bin = '.$this->db->escape($code);
        $this->db->where("link.share_code",$code);
        return $this->db->from(self::TB_SHARE_LINK." link")->join(self::TB_SHARE_LIST." list","list.lid=link.id AND list.type=1 AND list.fid=".$this->db->escape($fid))->where("list.type=1 AND list.fid=".$this->db->escape($fid))->limit(1)->get();
    }

    public function update_downloaded($fid,$code)
    {
        $sql = "UPDATE ".self::TB_SHARE_LINK." link LEFT JOIN ".self::TB_SHARE_LIST." list ON list.lid=link.id AND list.type=1 AND list.fid=".$this->db->escape($fid)."LEFT JOIN ".self::TB_FILE." file ON file.id=list.fid SET link.download_count=link.download_count+1,file.downloaded=1 WHERE link.share_code = ".$this->db->escape($code);
        return $this->db->query($sql);
    }

    /**
     * 从分享列表中移除文件
     * @param $uid
     * @param $fids
     * @param string $type 类型1文件,2文件夹
     */
    public function delete_sharelist_file($uid, $fids, $type="1") {
        if (empty($uid) || empty($fids))
            return;

        if (! is_array($fids)) $fids = explode(',', $fids);

        $this->db->where('type', $type);
        $this->db->where_in('fid', $fids);

        return $this->db->delete(self::TB_SHARE_LIST);
    }

    /**
     * 清理分享代码
     * @return mixed
     */
    public function clear_sharecode() {
        $delete_sql = 'DELETE n.* FROM u_share_link n LEFT JOIN u_share_list s ON s.lid = n.id WHERE ISNULL(s.lid)';
        return $this->db->query($delete_sql);
    }
}
 
/* End of file share_page_model.php */
/* Location:  */